package com.tulingxueyuan.mall.modules.pms.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tulingxueyuan.mall.common.api.CommonPage;
import com.tulingxueyuan.mall.common.api.CommonResult;
import com.tulingxueyuan.mall.modules.pms.model.*;
import com.tulingxueyuan.mall.modules.pms.service.PmsProductAttributeCategoryService;
import com.tulingxueyuan.mall.modules.pms.service.PmsProductAttributeService;
import com.tulingxueyuan.mall.modules.pms.service.PmsProductCategoryAttributeRelationService;
import com.tulingxueyuan.mall.modules.pms.service.PmsProductCategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 产品属性分类表 前端控制器
 * </p>
 *
 * @author SiXiaoLong
 * @since 2022-10-13
 */
@RestController
@RequestMapping("/productAttribute/category")
public class PmsProductAttributeCategoryController {
    @Autowired
    PmsProductAttributeCategoryService pmsProductAttributeCategoryService;

    @Autowired
    PmsProductAttributeService pmsProductAttributeService;

    @Autowired
    PmsProductCategoryService pmsProductCategoryService;


    @GetMapping("/list")
    public CommonResult list(Integer pageNum, Integer pageSize){
        Page<PmsProductAttributeCategory> page = new Page<>(pageNum,pageSize);
        pmsProductAttributeCategoryService.page(page);
        if (pageNum > page.getPages()){
            page = new Page<>((int)page.getPages(),pageSize);
            pmsProductAttributeCategoryService.page(page);
        }
        return CommonResult.success(CommonPage.restPage(page));
    }
/**
 * 筛选属性：
 */
    @GetMapping("/list/withAttr")
    @Transactional
    public CommonResult listWithAttr(){
//        BeanUtils.copyProperties(PmsProductAttributeCategory,PmsProductAttributeCategoryDto);
        List<PmsProductAttributeCategory> list = pmsProductAttributeCategoryService.list();

        List<PmsProductAttributeCategoryDto> list1 = list.stream().map(e -> {
            PmsProductAttributeCategoryDto dto = new PmsProductAttributeCategoryDto();
            BeanUtils.copyProperties(e,dto);
            LambdaQueryWrapper<PmsProductAttribute> lqw = new LambdaQueryWrapper<>();
            lqw.eq(PmsProductAttribute::getProductAttributeCategoryId,e.getId());
            List<PmsProductAttribute> productAttributeList = pmsProductAttributeService.list(lqw);
            dto.setProductAttributeList(productAttributeList);
            return dto;
        }).collect(Collectors.toList());
        return CommonResult.success(list1);
    }

    @PostMapping("/create")
    public CommonResult create(@RequestBody PmsProductAttributeCategory pmsProductAttributeCategory){
        pmsProductAttributeCategoryService.save(pmsProductAttributeCategory);
        return CommonResult.success("创建成功");
    }

    @PostMapping("/update/{id}")
    public CommonResult update(@PathVariable Long id,@RequestBody PmsProductAttributeCategory pmsProductAttributeCategory){
        pmsProductAttributeCategory.setId(id);
        pmsProductAttributeCategoryService.updateById(pmsProductAttributeCategory);
        return CommonResult.success("修改成功");
    }
    @GetMapping("/delete/{id}")
    public CommonResult update(@PathVariable Long id){
        pmsProductAttributeCategoryService.removeById(id);
        return CommonResult.success("删除成功");
    }

}

